package com.tang.crm.controller;

import com.tang.crm.base.BaseController;
import com.tang.crm.base.ResultInfo;
import com.tang.crm.dto.CustomerReprieveDto;
import com.tang.crm.entity.CustomerReprieve;
import com.tang.crm.service.CustomerReprieveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 客户暂存数据管理
 * @author SHIRELY阳
 */
@Controller
@RequestMapping("/customer_rep")
public class CustomerReprieveController extends BaseController {

    @Autowired
    private CustomerReprieveService customerReprieveService;

    /**
     * 分页查询
     * @param customerReprieveDto
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> selectByParams(CustomerReprieveDto customerReprieveDto) {
        return customerReprieveService.selectByParams(customerReprieveDto);
    }

    /**
     * 打开添加/修改暂缓数据的页面
     * @param lossId
     * @param request
     * @param id
     * @return
     */
    @RequestMapping("/toAddOrUpdateCustomerReprPage")
    public String toAddOrUpdateCustomerReprPage(Integer lossId, HttpServletRequest request, Integer id) {
        // 将流失客户ID存到作用域中
        request.setAttribute("lossId", lossId);
        // 判断id是否为空
        if (id != null) {
            request.setAttribute("customerRep", customerReprieveService.getById(id));
        }
        return "customerLoss/customer_rep_add_update";
    }


    /**
     * 添加暂缓数据
     * @param customerReprieve
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public ResultInfo addCustomerRepr(CustomerReprieve customerReprieve) {
        customerReprieveService.addCustomerRepr(customerReprieve);
        return success("添加暂缓数据成功");
    }

    /**
     * 更新暂缓数据
     * @param customerReprieve
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    public ResultInfo updateCustomerRepr(CustomerReprieve customerReprieve) {
        customerReprieveService.updateCustomerRepr(customerReprieve);
        return success("更新暂缓数据成功");
    }

    /**
     * 删除暂缓数据
     * @param id
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public ResultInfo deleteCustomerRepr(Integer id) {
        customerReprieveService.deleteCustomerRepr(id);
        return success("删除暂缓数据成功");
    }
}
